#include "mypushbutton.h"
#include <QMessageBox>
#include <QDebug>
#include <QPropertyAnimation>

MyPushButton::MyPushButton(QWidget *parent) : QPushButton(parent)
{

}
MyPushButton::MyPushButton(const QString& normalImg,const QString& pressImg )
{
    this->normalImg = normalImg;
    this->pressImg = pressImg;
    QPixmap pix ;
    int ret = pix.load(normalImg);
    if(!ret)
    {
        qDebug()<<"加载图片失败！url = "<<normalImg;
        QMessageBox::critical(this,"错误","加载按钮图片失败");
    }
    //设置图片固定尺寸
    this->setFixedSize(pix.width(),pix.height());
    //设置样式 边缘都是0像素，填充满。
    this->setStyleSheet("QPushButton {border:0px}");
    //
    this->setIcon(pix);
    //
    this->setIconSize(QSize(pix.width(),pix.height()));
    //添加label
    addLabel();
}

void MyPushButton::zoomup()
{
    //用动画实现
    QPropertyAnimation * anim = new QPropertyAnimation(this,"geometry");
    anim->setDuration(500);

    anim->setStartValue(QRect(this->x(),this->y(),this->width(),this->height()));
    anim->setEndValue(QRect(this->x(),this->y()+ 10,this->width(),this->height()));

    anim->setEasingCurve(QEasingCurve::OutBounce);
    anim->start();
    qDebug()<<"zoomup";

}
void MyPushButton::zoomdown()
{
    QPropertyAnimation * anim1 = new QPropertyAnimation(this,"geometry");
    anim1->setDuration(500);

    anim1->setStartValue(QRect(this->x(),this->y()+10,this->width(),this->height()));
    anim1->setEndValue(QRect(this->x(),this->y(),this->width(),this->height()));

    anim1->setEasingCurve(QEasingCurve::OutBounce);
    anim1->start();
    qDebug()<<"zoomdown";
}
void MyPushButton::mousePressEvent(QMouseEvent *event)
{
    //按下时显示不同的图片
    if(pressImg!= "")
    {
        QPixmap pixm;
        if(pixm.load(pressImg))
        {
            this->setFixedSize(pixm.width(),pixm.height());
            this->setStyleSheet("QPushButton{border:0px;}");
            this->setIcon(pixm);
            this->setIconSize(QSize(pixm.width(),pixm.height()));
        }else
        {
            qDebug()<<"加载图片失败 pressImg = "<<pressImg;
        }
    }
    qDebug()<<"按下事件 pressImg = "<<pressImg;
    //交给父类执行按下事件
    //QPushButton::mousePressEvent(event);
}
void MyPushButton::mouseReleaseEvent(QMouseEvent *event)
{
    //释放时显示其它图片
    if(normalImg!="")
    {
        QPixmap pixm;
        if(pixm.load(normalImg))
        {
            this->setFixedSize( pixm.width(), pixm.height() );
            this->setStyleSheet("QPushButton{border:0px;}");
            this->setIcon(pixm);
            this->setIconSize(QSize(pixm.width(),pixm.height()));

        }else
        {
            qDebug()<<"加载图片失败 normalImg = "<<normalImg;
        }

    }
    qDebug()<<"鼠标释放事件 normalImg = "<<normalImg;
    //交给父类执行按下事件
    //QPushButton::mousePressEvent(event);
    emit QPushButton::clicked();

}
void MyPushButton::addLabel()
{
    label = new QLabel(this);
    //鼠标穿透属性
    label->setAttribute(Qt::WidgetAttribute::WA_TransparentForMouseEvents);
    //设置大小
    label->setFixedSize(this->width(),this->height());
    //文本居中
    label->setAlignment(Qt::AlignCenter|Qt::AlignHCenter);
    //label->setText("测试");
}
void MyPushButton::setLabelText(QString level)
{
    label->setText(level);
}
